package com.serviceImpl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mapper.TableHistoryMapper;
import com.mapper.TradeMapper;
import com.mapper.UsersMapper;
import com.mapper.WtdMapper;
import com.pojo.OUser;
import com.pojo.Trade;
import com.pojo.Wtd;
import com.pojo.platform;
import com.service.TradeService;
import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;

import fun.pub;

@Service(value = "tradeService")
public class TradeServiceImpl implements TradeService {
	@Resource
	private TradeMapper tradeMapper;
	@Resource
	private WtdMapper wtdMapper;
	@Resource
	private TableHistoryMapper tableHistoryMapper;
	@Resource
	private UsersMapper usersMapper;

	public TradeServiceImpl() {
		// TODO Auto-generated constructor stub
	}

	// 卖方提交委托单的时候,需要先减去库存,然后,在添加委托单
	@Override
	public void matchinsert(Map<String, Object> param) {
		// TODO Auto-generated method stub
		wtdMapper.matchsale1(param);// 冻结卖方需要出售的商品数量
		wtdMapper.insertwtd(param);// 插入委托单数据
	}

	// 实时成交并插入委托单
	// 2017.12.18 11:11 yy 添加事务处理
	@Override
	public void insertbuy(HashMap tests) {
		List<Map> list = tradeMapper.selecttradeprice((String) tests.get("goods"));// 查询符合要求的卖方提交单
		if (list.size() == 0) {
			wtdMapper.insertwtd(tests);
		} else {
			BigDecimal wprice = (BigDecimal) tests.get("wprice");// 买方的委托价
			int wc = (int) tests.get("wcount");// 买方的委托数量
			int cwc = 0;// 定义成交量的变量
			int tcount = (int) tests.get("tcount");// 实时交易后剩余的商品数量
			wtdMapper.insertwtd(tests);
			wc = wc - tcount;// 剩余
			for (Map a : list) { // 小到大
				BigDecimal wprice1 = (BigDecimal) a.get("wprice");// 卖方的委托价
				int wc1 = (int) a.get("wcount");
				int tc1 = (int) a.get("tcount");
				wc1 = wc1 - tc1;
				// 当当前价格比列表价大
				if (wprice.compareTo(wprice1) >= 0) {
					if (wc > wc1) {// 委托量大于卖方的数量
						cwc = wc1;// 成交量
					} else {
						cwc = wc;
					}
					wc = wc - cwc;// 剩余
					BigDecimal zpost = wprice1.multiply(BigDecimal.valueOf(cwc));// 成交的总金额
					BigDecimal fee = zpost.multiply(new BigDecimal("0.003"));
					BigDecimal amoney = fee.multiply(new BigDecimal("2"));// 平台获得的手续费

					long wid1 = (long) tests.get("id");// 买方的委托单编号
					long wid2 = (long) a.get("wid");// 卖方的委托单编号

					Date now = new Date();
					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String time = dateFormat.format(now);

					// 需要获得利润
					HashMap<String, Object> param = new HashMap<String, Object>();
					param.put("UserID", a.get("UserID"));// 卖方的会员编号
					param.put("goods", a.get("goods"));// 商品名称
					param.put("allnum", cwc);// 成交量
					param.put("tradeprice", wprice1);// 成交价
					param.put("tradetime", time);// 成交时间
					param.put("wid1", wid1);// 买方的委托单编号
					param.put("wid2", wid2);// 卖方的委托单编号
					HashMap<String, Object> chengjiaolr = queueReduce(param);
					BigDecimal comsumezh2 = (BigDecimal) chengjiaolr.get("xfblr");// 消费币的利润
					BigDecimal recast = (BigDecimal) chengjiaolr.get("ftlr");// 复投的利润

					BigDecimal consume = zpost.subtract(fee);// 卖方获得的钱(毛利)
					BigDecimal consume2 = zpost.add(fee);// 买方需要花费的钱
					BigDecimal comsumezh1 = consume2;// 买方的消费

					// 获得买方账户的原有的余额
					OUser u = usersMapper.selectmember((String) tests.get("UserID"));
					HashMap<String, Object> sss1 = new HashMap<String, Object>();
					sss1.put("encash", u.getEncash());
					BigDecimal buyyue = (BigDecimal) sss1.get("encash");// 买方账户的余额
					BigDecimal buymoney = buyyue.subtract(consume2);
					// 获得卖方账户的原有的余额
					OUser u1 = usersMapper.selectmember((String) a.get("UserID"));
					HashMap<String, Object> sss = new HashMap<String, Object>();
					sss.put("encash", u1.getEncash());
					BigDecimal saleyue = (BigDecimal) sss.get("encash");// 卖方账户的余额
					BigDecimal sr = consume.subtract((comsumezh2.add(recast)));
					BigDecimal encash = sr;// 卖方的收入(扣除复投,消费币,手续费)
					BigDecimal salemoney = saleyue.add(sr);// 卖方账户余额+获得的毛利-消费币的利润-复投的利润

					HashMap<String, Object> buymx = new HashMap<String, Object>();
					buymx.put("wid", wid1);
					buymx.put("UserID", tests.get("UserID"));
					buymx.put("UserIDs", a.get("UserID"));
					buymx.put("encash", comsumezh1);
					buymx.put("tcount", cwc);
					buymx.put("income", 0);
					buymx.put("repay", zpost);
					buymx.put("yue", buymoney);// 要放到交易明细表里trecord
					buymx.put("goods", tests.get("goods"));
					buymx.put("tratime", time);// 交易时间
					buymx.put("fee", fee);
					buymx.put("ttime", time);
					buymx.put("obv", cwc);
					buymx.put("wprice", wprice1);

					HashMap<String, Object> salemx = new HashMap<String, Object>();
					salemx.put("consumezh", comsumezh2);
					salemx.put("recast", recast);
					salemx.put("encash", encash);
					salemx.put("tcount", cwc);
					salemx.put("wid", wid2);
					salemx.put("UserID", a.get("UserID"));
					salemx.put("income", zpost);
					salemx.put("repay", 0);
					salemx.put("goods", tests.get("goods"));
					salemx.put("yue", salemoney);
					salemx.put("tratime", time);
					salemx.put("fee", fee);

					HashMap<String, Object> trademx = new HashMap<String, Object>();
					trademx.put("wid1", wid1);
					trademx.put("UserID1", tests.get("UserID"));
					trademx.put("wid2", wid2);
					trademx.put("UserID2", a.get("UserID"));
					trademx.put("zpost", zpost);
					trademx.put("ttime", time);
					trademx.put("beizhu", cwc);
					trademx.put("goods", tests.get("goods"));
					trademx.put("obv", cwc);
					trademx.put("wprice", wprice1);
					trademx.put("amoney", amoney);
					Date shijian = new Date();
					SimpleDateFormat dFs = new SimpleDateFormat("yyyy-MM-dd");
					String sj = dFs.format(shijian);
					trademx.put("tradate", sj);

					match(trademx, buymx, salemx);

					if (wc == 0) {
						break;
					}
				}
			}
		}
	}

	// 2017.12.18 11:11 yy 添加事务处理
	@Override
	public void insertsale(HashMap tests) {
		List<Map> list = tradeMapper.selecttradeprice2((String) tests.get("goods"));
		if (list.size() == 0) {
			tests.put("djgoodnum", (int) tests.get("wcount"));// 获得委托单的数量，直接减少
			matchinsert(tests);

		} else {
			BigDecimal wprice = (BigDecimal) tests.get("wprice");// 卖方委托价
			int wc = (int) tests.get("wcount");// 卖方委托数量
			int cwc = 0;
			int tcount = (int) tests.get("tcount");// 实时交易后剩余的商品数量
			tests.put("djgoodnum", wc);
			matchinsert(tests);
			wc = wc - tcount;// 剩余
			for (Map a : list) { // 买入价格 从大到小,卖的越高,越好
				BigDecimal wprice1 = (BigDecimal) a.get("wprice");// 买方的价格
				int wc1 = (int) a.get("wcount");// 买方的委托数量
				int tc1 = (int) a.get("tcount");// 买方的剩余数量
				wc1 = wc1 - tc1;// 买方可以交易的数量
				if (wprice.compareTo(wprice1) <= 0) {// 当当前卖价格比列表买价小
					if (wc > wc1) {// 卖的数量大于买的数量
						cwc = wc1;// 成交量
					} else {
						cwc = wc;
					}
					wc = wc - cwc;// 剩余

					BigDecimal zpost = wprice1.multiply(BigDecimal.valueOf(cwc));// 交易的金额
					BigDecimal fee = zpost.multiply(new BigDecimal("0.003"));
					BigDecimal amoney = fee.multiply(new BigDecimal("2"));// 平台获得的手续费

					long wid1 = (long) a.get("wid");// 买方的委托单编号
					long wid2 = (long) tests.get("id");// 卖方的委托单编号

					Date now = new Date();
					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String time = dateFormat.format(now);

					BigDecimal consume = zpost.subtract(fee);// 卖方获得的钱(毛利)
					BigDecimal consume2 = zpost.add(fee);// 买方花的钱
					BigDecimal comsumezh1 = consume2;// 买方的消费

					// 需要获得成本
					HashMap<String, Object> param = new HashMap<String, Object>();
					param.put("UserID", tests.get("UserID"));
					param.put("goods", tests.get("goods"));
					param.put("allnum", cwc);//
					param.put("tradeprice", wprice1);//
					param.put("tradetime", time);
					param.put("wid1", wid1);
					param.put("wid2", wid2);
					HashMap<String, Object> chengjiaolr = queueReduce(param);

					BigDecimal comsumezh2 = (BigDecimal) chengjiaolr.get("xfblr");// 消费币的利润
					BigDecimal recast = (BigDecimal) chengjiaolr.get("ftlr");// 复投的利润

					// 获得买方账户的原有的余额
					OUser u = usersMapper.selectmember((String) a.get("UserID"));
					HashMap<String, Object> sss1 = new HashMap<String, Object>();
					sss1.put("encash", u.getEncash());
					BigDecimal buyyue = (BigDecimal) sss1.get("encash");
					BigDecimal buymoney = buyyue.subtract(consume2);

					// 获得卖方账户的原有的余额
					OUser u1 = usersMapper.selectmember((String) tests.get("UserID"));
					HashMap<String, Object> sss = new HashMap<String, Object>();
					sss.put("encash", u1.getEncash());
					BigDecimal saleyue = (BigDecimal) sss.get("encash");
					BigDecimal sr = consume.subtract((comsumezh2.add(recast)));// 卖方的收入(扣除复投,消费币,手续费)
					BigDecimal salemoney = saleyue.add(sr);// 卖方账户余额+获得的毛利-消费币的利润-复投的利润

					HashMap<String, Object> buymx = new HashMap<String, Object>();
					buymx.put("encash", comsumezh1);
					buymx.put("tcount", cwc);
					buymx.put("wid", wid1);
					buymx.put("UserID", a.get("UserID"));
					buymx.put("income", 0);
					buymx.put("repay", zpost);
					buymx.put("goods", a.get("goods"));
					buymx.put("yue", buymoney);
					buymx.put("tratime", time);
					buymx.put("fee", fee);
					buymx.put("ttime", time);
					buymx.put("obv", cwc);
					buymx.put("wprice", wprice1);
					buymx.put("UserIDs", tests.get("UserID"));

					HashMap<String, Object> salemx = new HashMap<String, Object>();
					salemx.put("consumezh", comsumezh2);
					salemx.put("recast", recast);
					salemx.put("encash", sr);
					salemx.put("tcount", cwc);
					salemx.put("wid", wid2);
					salemx.put("UserID", tests.get("UserID"));
					salemx.put("income", zpost);
					salemx.put("repay", 0);
					salemx.put("goods", tests.get("goods"));
					salemx.put("yue", salemoney);
					salemx.put("tratime", time);
					salemx.put("fee", fee);

					HashMap<String, Object> trademx = new HashMap<String, Object>();
					trademx.put("wid1", wid1);
					trademx.put("UserID1", a.get("UserID"));
					trademx.put("wid2", wid2);
					trademx.put("UserID2", tests.get("UserID"));
					trademx.put("zpost", zpost);
					trademx.put("ttime", time);
					trademx.put("beizhu", cwc);
					trademx.put("goods", tests.get("goods"));
					trademx.put("obv", cwc);
					trademx.put("wprice", wprice1);
					trademx.put("amoney", amoney);
					Date shijian = new Date();
					SimpleDateFormat dFs = new SimpleDateFormat("yyyy-MM-dd");
					String sj = dFs.format(shijian);
					trademx.put("tradate", sj);
					match(trademx, buymx, salemx);

					if (wc == 0) {
						break;
					}
				}
			}
		}
	}

	public void match(Map<String, Object> trademx, Map<String, Object> buymx, Map<String, Object> salemx) {
		// TODO Auto-generated method stub
		tradeMapper.inserttradeinfo(trademx);// 插入交易明细

		tradeMapper.insertgoodBank(buymx);// 买方:将交易信息插入到商品库存表
		int count=tradeMapper.selectclientgoods(buymx);//查询该用户是否有这个商品的相关信息
		if(count==0) {
			tradeMapper.insertclientgoods(buymx);//买方：增加商品明细表
		}
		else {
			tradeMapper.updatebuycg(buymx);// 增加买方的商品数量
		}
		
		tradeMapper.updatebuyencash(buymx);// 减少买方的金钱
		ycrwtdhis((long) buymx.get("wid"));// 将要修改的wtd插入到委托单历史表中
		tradeMapper.matchbuy3(buymx);// 更新委托单的数量
		tradeMapper.matchbuy5(buymx);// 插入挂买记录

		tradeMapper.updatesalecg(salemx);// 减少卖方的商品数量，还原冻结数量
		tradeMapper.matchsale2(salemx);// 更新卖方的账户余额(需要扣除手续费)
		// 查询当前委托单原来的数据,插入到委托单历史表中 2017.12.7 09:46 yy
		ycrwtdhis((long) salemx.get("wid"));
		tradeMapper.matchbuy3(salemx);// 更新委托单的数量
		tradeMapper.matchbuy5(salemx);// 插入挂售记录

		// 当tcount=wcount时会删除委托单,在删除之前需要查到这个表,然后,添加到历史表中 2017.12.8 15:05 yy
		List l = tableHistoryMapper.selectyscwtd();
		for (int i = 0; i < l.size(); i++) {
			// 获得wtd表中的wcount tcount
			Wtd ysc = (Wtd) l.get(i);
			HashMap<String, Object> yscw = new HashMap<String, Object>();
			yscw.put("UserID", ysc.getUserID());
			yscw.put("ttype", ysc.getTtype());
			yscw.put("goods", ysc.getGoods());
			yscw.put("wcount", ysc.getWcount());
			yscw.put("wprice", ysc.getWprice());
			yscw.put("consume", ysc.getConsume());
			yscw.put("wtime", ysc.getWtime());
			yscw.put("tcount", ysc.getTcount());
			yscw.put("state", ysc.getState());
			tableHistoryMapper.insertwtdHistory(yscw);
		}

		// 删除已交易成功的委托单 2017/12/1 11:05 yy
		tradeMapper.deletetradewtd();

		// 更新平台账户余额时,先查询,插入到平台历史表中 2017.12.7 09:35 yy
		platform pf = tableHistoryMapper.selectplatformhis();
		HashMap<String, Object> pf1 = new HashMap<String, Object>();
		pf1.put("amoney", pf.getAmoney());
		pf1.put("autostate", pf.getAutostate());
		tableHistoryMapper.insertplatformhis(pf1);
		tradeMapper.matchbuy4(trademx);// 更新平台余额

		// 查询今天是否有该商品的交易记录
				Trade tt = tradeMapper.selectTratable(trademx.get("goods").toString());
				HashMap<String, Object> trade = tt(trademx.get("goods").toString());
				HashMap<String, Object> trade1 = new HashMap<String, Object>();
				trade1.put("closeprice", trade.get("closeprice"));
				trade1.put("tcount", trade.get("tcount"));// 最低的成交量
				trade1.put("goods", trademx.get("goods"));
				trade1.put("sellcount", 1);
				trade1.put("fee", trademx.get("amoney"));// 每笔交易之后,平台获得的手续费是卖方+买方的手续费之和
				trade1.put("obv", trademx.get("obv"));// 成交数量,需要累加 2017.12.8 13:54 yy
				trade1.put("tradate", trademx.get("tradate"));
				if (tt == null) {
					tradeMapper.insertTratable(trade1);
				} else {
					// 在更改tradetable之前,先将原来的数据插到历史表中 2017.12.7 9:17 yy
					Trade st = tradeMapper.selectTratable(trademx.get("goods").toString());
					HashMap<String, Object> tt1 = new HashMap<String, Object>();
					tt1.put("closeprice", st.getCloseprice());
					tt1.put("fee", st.getFee());
					tt1.put("sellcount", st.getSellcount());
					tt1.put("tcount", st.getTcount());
					tt1.put("goods", st.getGoods());
					tt1.put("obv", st.getObv());
					tt1.put("tradate", st.getTradate());
					tableHistoryMapper.insertTratableHistory(tt1);
					tradeMapper.updateTratable(trade1);
				}
		// 查询当前日期的统计数据
		Trade td = tradeMapper.tradestatis1(trademx.get("goods").toString());
		HashMap<String, Object> tradestatis = t(trademx.get("goods").toString());	
		if (td == null) {
			tradestatis.put("vol", trademx.get("obv"));
			tradestatis.put("fee", trademx.get("amoney"));
			tradeMapper.tradestatis2(tradestatis);
		} else {
			tradestatis.put("vol", trademx.get("obv"));
			tradestatis.put("fee", trademx.get("amoney"));
			tradeMapper.tradestatis5(tradestatis);
		}
		
		// 查询当前每五分钟的的统计数据
		String ontime = pub.getOntime();
		HashMap<String, Object> aa = new HashMap<String, Object>();
		aa.put("ontime", ontime);
		aa.put("goods", trademx.get("goods"));
		Trade td9 = tradeMapper.tradeelif(aa);
		HashMap<String, Object> tradesstatis = t1(trademx.get("goods").toString());
		if (td9 == null) {
			tradesstatis.put("vol", trademx.get("obv"));
			tradeMapper.tradestatis10(tradesstatis);
		} else {
			// 更新
			tradesstatis.put("vol", trademx.get("obv"));
			tradeMapper.tradestatis11(tradesstatis);
		}

	}

	// 平台交易日志的信息
	public HashMap<String, Object> tt(String goods) {
		// 查询最近的一笔成交价格，作为收盘价格
		BigDecimal td4 = tradeMapper.tradestatis4(goods);
		int x = tradeMapper.selecttradeinfomx(goods);// 查询交易记录表里是否有该商品的信息
		int td6;// 最小的交易量
		if (x == 0) {
			td6 = 0;
		} else {
			td6 = tradeMapper.tradestatis6(goods);
		}
		if (td4 == null) {
			td4 = BigDecimal.valueOf(0.0);
		}
		HashMap<String, Object> trade = new HashMap<String, Object>();
		trade.put("closeprice", td4);
		trade.put("tcount", td6);
		return trade;
	}

	// 查询每日开盘价格，收盘价格，最高价格，最低价格
	public HashMap<String, Object> t(String goods) {		
		BigDecimal td3 = tradeMapper.tradestatis3(goods);// 查询昨天的收盘价格		
		BigDecimal td4 = tradeMapper.tradestatis4(goods);// 查询最近的一笔成交价格，作为收盘价格		
		BigDecimal td41 = tradeMapper.tradestatis41(goods);// 查询今天该商品成交价格的最高价格		
		BigDecimal td42 = tradeMapper.tradestatis42(goods);// 查询今天该商品成交价格的最低价格
		if (td4 == null) {
			td4 = BigDecimal.valueOf(0.0);
		}
		if (td41 == null) {
			td41 = BigDecimal.valueOf(0.0);
		}
		if (td42 == null) {
			td42 = BigDecimal.valueOf(0.0);
		}
		Date date = new Date();
		SimpleDateFormat formatdate = new SimpleDateFormat("yyyy-MM-dd");
		String td1 = formatdate.format(date);
		HashMap<String, Object> tradestatis = new HashMap<String, Object>();
		tradestatis.put("everydate", td1);
		tradestatis.put("openprice", td3);
		tradestatis.put("closeprice", td4);
		tradestatis.put("maxprice", td41);
		tradestatis.put("minprice", td42);
		tradestatis.put("goods", goods);
		return tradestatis;
	}

	// 每分钟成交量的最低最高开盘收盘
	public HashMap<String, Object> t1(String goods) {		
		BigDecimal td7 = tradeMapper.tradestatis7(goods);// 查询昨天的收盘价格	
		BigDecimal td8 = tradeMapper.tradestatis8(goods);// 查询最近的一笔成交价格，作为收盘价格		
		BigDecimal td81 = tradeMapper.tradestatis81(goods);// 查询今天成交价格的最高价格		
		BigDecimal td82 = tradeMapper.tradestatis82(goods);// 查询今天成交价格的最低价格
		if (td8 == null) {
			td8 = BigDecimal.valueOf(0.0);
		}
		if (td81 == null) {
			td81 = BigDecimal.valueOf(0.0);
		}
		if (td82 == null) {
			td82 = BigDecimal.valueOf(0.0);
		}

		HashMap<String, Object> tradesstatis = new HashMap<String, Object>();
		String ontime = pub.getOntime();
		tradesstatis.put("ontime", ontime);
		tradesstatis.put("openprice", td7);
		tradesstatis.put("closeprice", td8);
		tradesstatis.put("maxprice", td81);
		tradesstatis.put("minprice", td82);
		tradesstatis.put("goods", goods);
		return tradesstatis;
	}

	// 思路进入到不同价格库存商品/逐一核减/范畴
	// 卖方:更改库存的数量,增加利润
	public HashMap<String, Object> queueReduce(Map<String, Object> param) {
		int allnum = (int) param.get("allnum");
		BigDecimal tradeprice = (BigDecimal) param.get("tradeprice");
		HashMap<String, Object> hashmap = new HashMap<String, Object>();
		hashmap.put("UserID", param.get("UserID"));
		hashmap.put("goods", param.get("goods"));
		// 1 根据会员编号和商品名称,取自有的，该商品的库存列表，按时间从小到大升序排列
		List<HashMap<String, Object>> list = (List<HashMap<String, Object>>) tradeMapper.getTradeList(hashmap);
		HashMap<String, Object> tr1 = new HashMap<String, Object>();
		tr1.put("UserID", param.get("UserID"));
		tr1.put("goods", param.get("goods"));
		tr1.put("widb", param.get("wid1"));
		tr1.put("wids", param.get("wid2"));
		tr1.put("wprice", tradeprice);// 成交价
		tr1.put("ttime", param.get("tradetime"));

		// 根据当前交易数量逐一合减列表中的数量,价格不一样,购买时间也是不一样
		int cjnum = 0;// 每一条成交量
		BigDecimal addli = BigDecimal.valueOf(0);
		int addcjl = 0;// 成交量的叠加

		List lizdy = new ArrayList();
		for (HashMap<String, Object> row : list) {
			BigDecimal wprice = (BigDecimal) row.get("wprice");// 卖方的成本价
			tr1.put("cbj", wprice);// 成本价
			int num = (int) row.get("obv");// 库存的数量
			int id = (int) row.get("id");

			if (allnum > num) {// 如果成交的数量大于这条库存数量
				cjnum = num;
				num = 0;
				allnum -= cjnum;// 求剩余的与买方的成交量
				addcjl += cjnum;// 成交量的叠加
				// 根据id,查到被修改的元数据,插入到库存的历史表中
				ycrgbhis(id);
				tradeMapper.deletegoodBank(id);
			} else {// 如果当前余下的成交数量小于库存数量
				num -= allnum;
				cjnum = allnum;
				allnum -= cjnum;
				BigDecimal lil = (tradeprice.subtract(wprice)).multiply(BigDecimal.valueOf(cjnum));// 成交量*(成交价-成本价)的利润
				addli = addli.add(lil);// 利润叠加
				ycrgbhis(id);
				addcjl += cjnum;// 成交量的叠加
				// 如果成交的数量大于库存的单笔数量,逐个修改库存
				HashMap<String, Object> p = new HashMap<String, Object>();
				p.put("id", id);
				p.put("obv", cjnum);
				tradeMapper.updategoodBank(p);

				if (num == 0) {
					// 将需要已经扣光库存的数据,在删除之前,插入到goodBank_his表中2017.12.8 15:05 yy
					ycrgbhis(id);
					tradeMapper.deletegoodBank(id);
				}
				break;
			}
			if (allnum == 0)
				break;
		}
		tr1.put("obv", addcjl);

		BigDecimal sumlr = addli;
		BigDecimal xfblr = addli.multiply(new BigDecimal("0.3"));
		BigDecimal ftlr = addli.multiply(new BigDecimal("0.1"));
		BigDecimal qxlr = addli.multiply(new BigDecimal("0.6"));
		if (addli.compareTo(new BigDecimal("0")) > 0) {
			tr1.put("sumlr", sumlr);
			tr1.put("xfblr", xfblr);
			tr1.put("ftlr", ftlr);
			tr1.put("qxlr", qxlr);

		} else {
			tr1.put("sumlr", new BigDecimal("0"));
			tr1.put("xfblr", new BigDecimal("0"));
			tr1.put("ftlr", new BigDecimal("0"));
			tr1.put("qxlr", new BigDecimal("0"));
		}
		tradeMapper.insertsaleIntrest(tr1);// 调用map接口写入
		return tr1;
	}

	public void ycrwtdhis(long id) {
		// 查询当前委托单原来的数据,插入到委托单历史表中 2017.12.7 09:46 yy
		Wtd wh = tableHistoryMapper.selectwtdHistory(id);
		HashMap<String, Object> wh1 = new HashMap<String, Object>();
		wh1.put("UserID", wh.getUserID());
		wh1.put("ttype", wh.getTtype());
		wh1.put("goods", wh.getGoods());
		wh1.put("wcount", wh.getWcount());
		wh1.put("wprice", wh.getWprice());
		wh1.put("consume", wh.getConsume());
		wh1.put("wtime", wh.getWtime());
		wh1.put("tcount", wh.getTcount());
		wh1.put("state", wh.getState());
		tableHistoryMapper.insertwtdHistory(wh1);
	}

	// 将需要更改的数据从goodBank表中查到,插入到goodBank_his表中 2017.12.7 10:36 yy
	public void ycrgbhis(int id) {
		Trade tgb = tableHistoryMapper.selectgoodBankHistory(id);
		HashMap<String, Object> tgb1 = new HashMap<String, Object>();
		tgb1.put("UserID", tgb.getUserID());
		tgb1.put("ttime", tgb.getTtime());
		tgb1.put("goods", tgb.getGoods());
		tgb1.put("obv", tgb.getObv());
		tgb1.put("wprice", tgb.getWprice());
		tgb1.put("UserIDs", tgb.getUserIDs());
		tableHistoryMapper.insertgoodBankHistory(tgb1);
	}

}
